package test;

import java.util.Vector;

import parser.DesPushParser;
import parser.GetGrupoParser;
import parser.GetMoreInfoClaseParser;
import parser.GetPeriodosClaseParser;
import parser.PausePushParser;
import parser.PushParser;
import parser.VersionParser;
import screens.Controller;
import webservice.WebServiceCaller;
import beans.Course;
import beans.Update;

public class GradeAnalyzer {
	
	private static GradeAnalyzer ga;

	private GradeAnalyzer(){
	}
	
	public static GradeAnalyzer getInstance(){
		if(ga==null){
			ga=new GradeAnalyzer();
		}
		return ga;
	}
	//private static String XML_MD5 = "";
	private WebServiceCaller ws = new WebServiceCaller();

	public String getLoginHash(String user, String pass) {
		String prehash = ws.getLoginHash(user, pass);
		if(prehash.equals("")){
			return "";
		}else{
			return prehash.substring(1, prehash.length()-1);
		}
	}

	public Vector getPeriodosClase(String hash){
		String xml = ws.getPeriodosClaseXml(hash);
		if (xml.equals("")){
			return null;
		} else {
			GetPeriodosClaseParser parser = new GetPeriodosClaseParser();
			return parser.parseXML(xml);
		}
	}

	public void getMoreInfoClase(Course c, String hash){
		new GetMoreInfoClaseParser().parseXML(c, ws.getMoreInfoClaseXml(hash, c.getIDAula()));
	}
	
	public void getGrupo(Course c, String hash){
		new GetGrupoParser().parseXML(c, ws.getGrupoXml(hash, c.getIDAula()));
	}

	public void suscribirPush(String hash, String pin) {
		Controller.getInstance().setPushStatus(new PushParser().parseXML(ws.suscribirPush(hash, pin)));
	}

	public void desSuscribirPush(String hash, String pin) {
		Controller.getInstance().setPushStatus(new DesPushParser().parseXML(ws.desSuscribirPush(hash, pin)));
	}

	public void pausePushStatus(String hash, String pin, int status) {
		boolean result = new PausePushParser().parseXML(ws.changePushStatus(hash, pin, status));
		Controller.getInstance().setPushStatus(result);
		System.out.println("***** push status result: "+result);		
	}

	public Update getVersion(String version) {
		return new VersionParser().parseJSON(ws.getCompatibleVersion(version));
	}

}
